﻿package fab.geom
{
	import flash.display.GraphicsPathWinding;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Drawer
	{
		static public function drawPoint( graphics:Graphics, p:Point, couleur:int=0xFF0000 ):void 
		{
			if ( p == null ) return;
			var _commands:Vector.<int> = new Vector.<int>();
			_commands.push( 1, 2, 1, 2 );
			var _coord:Vector.<Number> = new Vector.<Number>();
			_coord.push( p.x - 6, p.y, p.x + 6, p.y, p.x, p.y - 6, p.x, p.y + 6 );
			graphics.lineStyle( 0, couleur );
			//graphics.beginFill( couleur );
			graphics.drawPath( _commands, _coord, GraphicsPathWinding.NON_ZERO );	
		}	
		static public function drawLine( graphics:Graphics, ligne:Line, couleur:int=0xFF0000 ):void 
		{
			var left:Number = -1000;// where.x - ( where.stage.stageWidth - where.width ) / 2;
			var right:Number = +1000;//where.x + where.width + ( where.stage.stageWidth - where.width ) / 2;
			
			var _commands:Vector.<int> = new Vector.<int>();
			var _coord:Vector.<Number> = new Vector.<Number>();
			_commands.push( 1, 2 );
			if ( ligne.isVertical() )
			{
				//TODO draw line vertical : acces à stage.height ?
			}
			else {
				_coord.push( left, ligne.getY( left ), right, ligne.getY( right ) );
			}
			graphics.lineStyle( 0, couleur );
			graphics.drawPath( _commands, _coord, GraphicsPathWinding.NON_ZERO );			
		}
		static public function drawRectangle( graphics:Graphics, rect:Rectangle, couleur:int=0xFF0000 ):void 
		{
			graphics.drawRect( rect.x, rect.y, rect.width, rect.height );
		}
		static public function drawCircle( graphics:Graphics, centre:Point, rayon:Number, couleur:int=0xFF0000 ):void 
		{
			graphics.drawCircle( centre.x, centre.y, rayon );
		}
		static public function drawEllipse( graphics:Graphics, centre:Point, largeur:Number, hauteur:Number, couleur:int=0xFF0000 ):void 
		{
			graphics.drawEllipse( centre.x, centre.y, largeur, hauteur );
		}		
		static public function drawSegment( graphics:Graphics, p1:Point, p2:Point, couleur:int=0xFF0000 ):void 
		{
			var _commands:Vector.<int> = new Vector.<int>();
			_commands.push( 1, 2 );
			var _coord:Vector.<Number> = new Vector.<Number>();
			_coord.push( p1.x, p1.y, p2.x, p2.y );
			graphics.lineStyle( 0, couleur );
			graphics.drawPath( _commands, _coord, GraphicsPathWinding.NON_ZERO );	
		}
		static public function drawCurve( graphics:Graphics, vpoints:Vector.<Point>, couleur:int=0xFF0000 ):void 
		{
			var command_length:int =  1 + ( vpoints.length - 1 ) / 2;
			var _commands:Vector.<int> = new Vector.<int>( command_length , true );
			_commands[ 0 ] = 1;
			var i:int = 1
			while ( i < command_length ) _commands[ i++ ] = 3;
			var _coord:Vector.<Number> = new Vector.<Number>();
			for each ( var pp:Point in vpoints ) _coord.push( int(pp.x), int(pp.y) );
			graphics.lineStyle( 0, couleur );
			graphics.drawPath( _commands, _coord, GraphicsPathWinding.NON_ZERO );	
		}		
		static public function drawStar( graphics:Graphics, couleur:int=0xFF0000 ):void 
		{
			var _commands:Vector.<int> = new Vector.<int>();
			_commands.push( 1, 2, 2, 2, 2);
			var _coord:Vector.<Number> = new Vector.<Number>();
			_coord.push(66, 10, 23, 127, 122, 50, 10, 49, 109, 127);
			graphics.lineStyle( 0, couleur );
			graphics.beginFill( couleur );
			graphics.drawPath( _commands, _coord, GraphicsPathWinding.NON_ZERO );	
		}
		/*
		 * drawCone( where, x centre, y centre, rayon, arc en degre, start angle en degre )
		 * */
		static public function drawCone(graphics:Graphics, sx:Number, sy:Number, radius:Number, arc:Number, startAngle:Number=0):void 
		{
			var segAngle:Number;
			var angle:Number;
			var angleMid:Number;
			var numOfSegs:Number;
			var ax:Number;
			var ay:Number;
			var bx:Number;
			var by:Number;
			var cx:Number;
			var cy:Number;

			// Move the pen
			graphics.moveTo(sx, sy);

			// No need to draw more than 360
			if (Math.abs(arc) > 360) 
			{
				arc = 360;
			}

			numOfSegs = Math.ceil(Math.abs(arc) / 45);
			segAngle = arc / numOfSegs;
			segAngle = (segAngle / 180) * Math.PI;
			angle = (startAngle / 180) * Math.PI;

			// Calculate the start point
			ax = sx + Math.cos(angle) * radius;
			ay = sy + Math.sin(-angle) * radius;

			// Draw the first line
			graphics.lineTo(ax, ay);

			for (var i:int=0; i<numOfSegs; i++) 
			{
				angle += segAngle;
				angleMid = angle - (segAngle / 2);
				bx = sx + Math.cos(angle) * radius;
				by = sy + Math.sin(angle) * radius;
				cx = sx + Math.cos(angleMid) * (radius / Math.cos(segAngle / 2));
				cy = sy + Math.sin(angleMid) * (radius / Math.cos(segAngle / 2));
				graphics.curveTo(cx, cy, bx, by);
			}

			// Close the wedge
			graphics.lineTo(sx, sy);
		}			
	}
}